package com.boarsoft.boar.sys.biz;

import java.util.List;

import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.boar.sys.SysFuncBiz;
import com.boarsoft.boar.sys.entity.RoleFunc;
import com.boarsoft.boar.sys.entity.SysFunc;
import com.boarsoft.boar.sys.entity.SysRole;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

@Component("sysFuncBiz")
public class SysFuncBizImpl extends SimpleBizImpl implements SysFuncBiz {

	@Override
	@Transactional(readOnly = true)
	public PagedResult<SysFunc> list(String key, String orderBy, int pageNo, int pageSize) {
		StringBuffer sb = new StringBuffer();
		if (Util.strIsNotEmpty(key)) {
			key = StringEscapeUtils.escapeSql(key);
			sb.append(" and (code like '%").append(key);
			sb.append("%' or name like '%").append(key);
			sb.append("%' or type like '%").append(key);
			sb.append("%' or memo like '%").append(key);
			sb.append("%' or uri like '%").append(key);
			sb.append("%')");
		}
		int total = dao.getTotal(SysFunc.class, sb.toString());
		if (Util.strIsNotEmpty(orderBy)) {
			String ob = Util.array2str(orderBy.split(","), ",");
			sb.append(" order by ").append(ob);
		} else {
			sb.append(" order by code");
		}
		List<SysFunc> alt = dao.list(SysFunc.class, sb.toString(), pageNo, pageSize);
		return new PagedResult<SysFunc>(total, alt, pageNo, pageSize);
	}

	@Override
	@Transactional
	public Boolean add(SysFunc o) {
		if (dao.get(SysFunc.class, o.getCode()) == null) {
			dao.save(o);
			// 为超级管理员添加针对新创建的功能模块的权限
			RoleFunc rf = new RoleFunc();
			rf.setRoleId(SysRole.ADMIN_ID);
			rf.setFuncCode(o.getCode());
			dao.save(rf);
			return true;
		}
		return false;
	}

	@Override
	@Transactional
	public Boolean update(SysFunc o) {
		dao.merge(o);
		return true;
	}

	@Override
	@Transactional
	public Boolean delete(String code) {
		return dao.delete(SysFunc.class, code);
	}

	@Override
	@Transactional
	public SysFunc start(String code) {
		SysFunc o = dao.get(SysFunc.class, code);
		o.setStatus(SysFunc.STATUS_INUSE);
		return o;
	}

	@Override
	@Transactional
	public SysFunc stop(String code) {
		SysFunc o = dao.get(SysFunc.class, code);
		o.setStatus(SysFunc.STATUS_NOUSE);
		return o;
	}

	@Override
	@Transactional
	public void save(List<SysFunc> sysFuncs, List<RoleFunc> roleFuncs) {
		try {
			if (sysFuncs.size() > 0) {
				// 批量保存环境相关的sys_func数据
				dao.batchSave(sysFuncs, 100);
			}
			if (roleFuncs.size() > 0) {
				// 批量保存环境相关的role_func数据
				dao.batchSave(roleFuncs, 100);
			}
		} catch (Exception e) {
			log.error("Failed to batch save sys_func and role_func:", e);
		}
	}

	@Override
	@Transactional
	public void delete(List<SysFunc> lt) {
		if (lt.isEmpty()) {
			return;
		}
		// 在删除sys_func时，会根据关联一起删除role_func相关数据
		dao.batchDelete(lt, 100);
	}

	@Override
	@Transactional(readOnly = true)
	public SysFunc get(String code) {
		return dao.get(SysFunc.class, code);
	}
}
